package com.hyf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyf.mapper.BrandMapper;
import com.hyf.service.BrandService;
import com.hyf.pojo.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@SuppressWarnings("all")
//@SuppressWarnings("all")
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public void deleteById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public List<Brand> findByName(Brand brand) {
        Example example = new Example(Brand.class);

        Example.Criteria criteria = example.createCriteria();

        if (brand != null) {
            if (brand.getName() != null && !"".equals(brand.getName())) {
                criteria.andLike("name", "%" + brand.getName() + "%");
            }
        }
        return brandMapper.selectByExample(example);
    }

    @Override
    public PageInfo<Brand> findByPage(Integer page, Integer pageSize) {

        Page<Brand> brandPage = PageHelper.startPage(page, pageSize);

        List<Brand> brandList = brandMapper.selectAll();

        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);

        return pageInfo;
    }

    @Override
    public PageInfo<Brand> findPageByName(Integer page, Integer pageSize, Brand brand) {

        Page<Brand> brandPage = PageHelper.startPage(page, pageSize);

        Example example = new Example(Brand.class);

        Example.Criteria criteria = example.createCriteria();

        if (brand != null) {
            if (brand.getName() != null && !"".equals(brand.getName())) {
                criteria.andLike("name", "%" + brand.getName() + "%");
            }
        }
        List<Brand> brandList = brandMapper.selectByExample(example);

        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);

        return pageInfo;
    }
}
